#!/usr/bin/env python
# encoding: utf-8


from django.http import HttpResponse
from django.template import RequestContext, loader
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
from django.http import Http404
import os
from django.utils.encoding import smart_str
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser
import datetime

def index(request):
    template = loader.get_template('showinfo/index.html')

    return HttpResponse(template.render())


def showweather(request):
    template = loader.get_template('showinfo/weather.html')

    return HttpResponse(template.render())


def showidcard(request):
    template = loader.get_template('showinfo/idcard.html')

    return HttpResponse(template.render())


def phonenum(request):
    template = loader.get_template('showinfo/phonenumber.html')

    return HttpResponse(template.render())


def tuling(request):
    template = loader.get_template('showinfo/tuling.html')

    return HttpResponse(template.render())


def ocrimg(request):
    template = loader.get_template('showinfo/OCRImage.html')

    return HttpResponse(template.render())


def music(request):
    template = loader.get_template('showinfo/music.html')

    return HttpResponse(template.render())


@csrf_exempt
def fileupload(request):
    if request.method == 'POST':
        fname = ''
        timestr=datetime.datetime.now().strftime('%Y/%m/%d')
        basepath=os.path.join(r'/var/www/wordpress/wp-content/uploads/',timestr)
        if not os.path.exists(basepath):
            os.makedirs(basepath)
        for filename, file in request.FILES.iteritems():
            fname = filename
            savepath = os.path.join(basepath, filename)
            with open(savepath, 'wb+') as wfile:
                for chunk in file.chunks():
                    wfile.write(chunk)
        return HttpResponse('https://www.lylinux.org/'+'wp-content/uploads/'+timestr+'/' + fname)


    else:
        return HttpResponse("only for post")


def showimages(request):
    template = loader.get_template('showinfo/showimages.html')

    return HttpResponse(template.render())
